package ar.com.photo_admin.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import ar.com.photo_admin.Constants;
import ar.com.photo_admin.R;
import ar.com.photo_admin.domain.Event;
import ar.com.photo_admin.exception.UnreachableNetworkException;
import ar.com.photo_admin.services.Services;
import ar.com.photo_admin.services.ServiceLayer;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.PauseOnScrollListener;

public class EventListActivity extends ListActivity {
	
	private List<Event> events = new ArrayList<Event>();
	
	private ArrayAdapter<Event> adapter;
	
	private ServiceLayer webService;
	
	private DisplayImageOptions options;
	
	private ImageLoader imageLoader = ImageLoader.getInstance();
	
	private boolean instanceStateSaved;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.events);
		
		webService = Services.createServiceLayer(this);
		
		options = new DisplayImageOptions.Builder()
			.showStubImage(R.drawable.ic_stub)
			.showImageForEmptyUri(R.drawable.ic_empty)
			.cacheInMemory()
			.cacheOnDisc()
			.build();
		
		this.adapter = (new ArrayAdapter<Event>(this,
				R.layout.item_list_image, events) {
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent) {
				
				View row = convertView;

				if (row == null) {
					LayoutInflater inflater = getLayoutInflater();
					row = inflater.inflate(R.layout.item_list_image, parent, false);
				}
				
				final Event event = getItem(position);
				
				TextView name = (TextView) row.findViewById(R.id.text);
				name.setText(event.getName());
				 
				ImageView image = (ImageView) row.findViewById(R.id.image);
				imageLoader.displayImage(event.getCover().getThumbnailUrl(), image, options);
			    
				return row;
			}
		});
		
		this.getListView().setOnScrollListener(new PauseOnScrollListener(false, true));
		
		new DownloadEventsTask().execute();
		setListAdapter(adapter);
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		final Event event = adapter.getItem(position);
		Toast.makeText(this, event.getName(), Toast.LENGTH_SHORT).show();
		
		Intent intent = new Intent(this, AlbumListActivity.class);
		intent.putExtra(Constants.EVENT, event);
		
		startActivity(intent);
	}
	
	private class DownloadEventsTask extends AsyncTask<Void, Void, List<Event>> {

		private ProgressDialog dialog = null;
		
		private Boolean connectionProblem = false;
		
		@Override
		protected void onPreExecute() {
		
			dialog = new ProgressDialog(EventListActivity.this);
			dialog.setTitle(getString(R.string.loading));
			dialog.setMessage(getString(R.string.loading_events));
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			dialog.show();
		}
		
		@Override
		protected List<Event> doInBackground(Void... params) {
			
			List<Event> events = new ArrayList<Event>();
			
			try {
				events = webService.getEvents();
			} catch (UnreachableNetworkException e) {
				connectionProblem = true;
			}
			
			return events;
		}
		
		@Override
		protected void onPostExecute(List<Event> result) {

			events.clear();
			events.addAll(result);

			adapter.notifyDataSetChanged();
			
			this.dialog.dismiss();
			
			if (connectionProblem) 
				Toast.makeText(EventListActivity.this, R.string.connection_problem, Toast.LENGTH_SHORT).show();
		}
	}
	
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		instanceStateSaved = true;
	}

	@Override
	protected void onDestroy() {
		if (!instanceStateSaved) {
			imageLoader.stop();
		}
		super.onDestroy();
	}
}